# 数据获取模块
import requests
from lxml import etree
import json
import openpyxl
import time


# 定义一个方法，获取数据
# 获取数据的网址：https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner
def get_data_requests():
    url = 'https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
    }

    response = requests.get(url=url, headers=headers)
    # print(response.text)
    # 使用etree将字符串内容变成xpath可以分析的html格式
    str1 = etree.HTML(response.text)
    # 使用xpath语法匹配标签的内容将数据获取到
    str1 = str1.xpath('//script[@type="application/json"]/text()')[0]
    # print(str1)
    data = json.loads(str1)
    # print(data)
    return data


def china_total_data(data):
    # 获取中国各个省份的数据
    """
     data['component'][0]['caseList']
     area:省份
     confirmedRelative: 新增确诊
     unOverseasInputNewAdd：新增本土
     overseasInputRelative：新增境外
     asymptomaticRelative：新增无症状
     curConfirm：现有确诊
     curLocalConfirm：现有本土
     curOverseasInput：现有境外
     asymptomatic：现有无症状
     confirmed：累计确诊
     overseasInput：累计境外
     crued：累计治愈
     died：累计死亡
     relativeTime：当地时间
     diedRelative：累计死亡增量
     curedRelative：累计治愈增量
     curConfirmRelative：现有确诊增量

     '省份', '累计确诊人数', '累计死亡人数', '累计治愈人数', '现有确诊人数', '新增无症状', '累计确诊增量', '累计死亡增量', '累计治愈增量', '现有确诊增量', '更新时间'
     """
    # 创建中国疫情数据工作簿
    wb = openpyxl.Workbook()
    # 获取默认创建的工作表
    ws_china_province = wb.active
    # 对表进行改一个名字
    ws_china_province.title = "中国各个省份疫情数据"
    ws_china_province.column_dimensions['K'].width = 23
    # 给表添加字段
    ws_china_province.append(
        ['省份', '累计确诊人数', '累计死亡人数', '累计治愈人数', '现有确诊人数', '新增无症状', '累计确诊增量', '累计死亡增量', '累计治愈增量', '现有确诊增量', '更新时间'])
    # 获取省份数据
    provinces = data['component'][0]['caseList']
    # print(provinces)
    for province in provinces:
        # 获取每个省份的更新时间
        timestemp = province['updateTime']
        # 对时间戳进行格式化
        update_time = time.strftime('%Y-%m-%d %H-%M-%S', time.localtime(int(timestemp)))

        temp_list = [province['area'], province['confirmed'], province['died'], province['crued'],
                     province['curConfirm'], province['asymptomaticRelative'],
                     province['confirmedRelative'], province['diedRelative'], province['curedRelative'],
                     province['curConfirmRelative'],
                     update_time]
        # print(temp_list)
        ws_china_province.append(temp_list)

    # 获取中国省份下的各个市的数据
    """
    city:城市
    confirmed:累计确诊
    died:累计死亡
    crued:累计治愈
    curConfirm:现有确诊
    confirmedRelative:累计确诊增量
    asymptomaticRelative:新增无症状
    updateTime:更新时间
    {'city': '拉萨', 'confirmed': '752', 'died': '0', 'crued': '668', 'asymptomaticRelative': '15', 
    'confirmedRelative': '3', 'asymptomaticLocalRelative': '15', 'asymptomatic': '0', 'nativeRelative': '3', 
    'screeningPositive': '0', 'curAsymptomaticLocalRelative': '', 'updateTime': '1665629497', 'curConfirm': '84', 
    'noNativeRelativeDays': '', 'cityCode': '100', 'dangerousAreas': 
    {'subList': [{'area': '城关区功德林街道幸福社区（除部分点位，详见拉萨防疫通告第82号）', 'level': '中风险', 'isNew': 1}, 
    {'area': '城关区功德林街道拉鲁社区（除部分点位，详见拉萨防疫通告第82号）', 'level': '中风险', 'isNew': 1}, 
    {'area': '城关区娘热街道仁钦蔡村（除部分点位，详见拉萨防疫通告第82号）', 'level': '中风险', 'isNew': 1}, 
    {'area': '城关区娘热路幸福新村', 'level': '中风险', 'isNew': 1}, 
    {'area': '城关区扎细街道尼卓林社区琅赛9区北区拉萨防疫通告第82号', 'level': '中风险', 'isNew': 1}, 
    {'area': '城关区扎细街道扎细社区（除部分点位，详见拉萨防疫通告第82号）', 'level': '中风险', 'isNew': 1}, 
    {'area': '城关区纳金街道加荣社区（除部分点位，详见拉萨防疫通告第82号）', 'level': '中风险', 'isNew': 1}, 
    {'area': '城关区色拉北路圣城花园', 'level': '中风险', 'isNew': 1}, 
    {'area': '城关区金珠西路街道八一社区（除部分点位，详见拉萨防疫通告第82号）', 'level': '中风险', 'isNew': 1}, 
    {'area': '城关区金珠西路街道当巴社区（除部分点位，详见拉萨防疫通告第82号）', 'level': '中风险', 'isNew': 1}], 
    'moreUrl': 'http://m.ls.bendibao.com/news/yqdengji/?src=baidu', 'totalNum': 10, 'highLevelNum': 0, 'midLevelNum': 10}}
    """
    # 创建一张表进行存储各个市的疫情情况
    ws_citys = wb.create_sheet("中国各个市疫情数据")
    # 给字段
    ws_citys.append(['城市', '现有确诊', '累计确诊', '累计死亡', '累计治愈', '累计确诊增量', '新增无症状', '更新时间'])

    for province in provinces:
        for city in province['subList']:
            # 获取每个省份的更新时间
            timestemp2 = city['updateTime']
            # 对时间戳进行格式化
            update_time2 = time.strftime('%Y-%m-%d %H-%M-%S', time.localtime(int(timestemp2)))
            temp_list2 = [city['city'], city['curConfirm'], city['confirmed'], city['died'],
                          city['crued'], city['confirmedRelative'], city['asymptomaticRelative'], update_time2]
            # print(temp_list2)
            # 有些key对应的值是'',没有数据，如果直接放到表中就是空，将来可能会影响到可视化的步骤
            # 所以要做脏数据的处理，如果是''，那么就用0代替

            for i in range(len(temp_list2)):
                if temp_list2[i] == '':
                    temp_list2[i] = '0'

            ws_citys.append(temp_list2)
            # print(temp_list2)

    # 获取中国疫情数据更新时间
    ws_china_update_time = wb.create_sheet("中国疫情数据更新时间")
    ws_china_update_time.column_dimensions['A'].width = 23
    ws_china_update_time.append(['更新时间'])
    ws_china_update_time.append([data['component'][0]['mapLastUpdatedTime']])

    wb.save("COVID-19-china.xlsx")
    print("中国疫情数据保存完毕！！！")


def global_total_data(data):
    """
    caseOutsideList
    area:国家
    confirmed:累计确诊
    died:累计死亡
    crued:累计治愈
    curConfirm:现有确诊
    confirmedRelative:累计确诊增量
    relativeTime: 当地时间
    表字段：'国家', '累计确诊', '累计死亡', '累计治愈', '现有确诊', '累计确诊增量'
    """
    # 创建一个工作簿，存储全球疫情数据
    wb = openpyxl.Workbook()
    ws_countries = wb.active
    ws_countries.title = "全球各个国家疫情数据"
    ws_countries.append(['国家', '累计确诊', '累计死亡', '累计治愈', '现有确诊', '累计确诊增量', '当地时间'])
    countries = data['component'][0]['caseOutsideList']
    for country in countries:
        relative_time = country['relativeTime']
        relative_time = time.strftime('%Y-%m-%d %H-%M-%S', time.localtime(int(relative_time)))

        temp_list = [country['area'], country['confirmed'], country['died'], country['crued'],
                     country['curConfirm'], country['confirmedRelative'], relative_time]
        # print(temp_list)
        for i in range(len(temp_list)):
            if temp_list[i] == '':
                temp_list[i] = '0'

        ws_countries.append(temp_list)

    # 保存各个州的疫情数据，将每一个洲为一个表
    continents = data['component'][0]['globalList']
    # print(continents)
    # 遍历列表获取每一个洲的信息
    for continent in continents:
        ws_continent = wb.create_sheet(continent['area'] + "疫情数据")
        ws_continent.append(['国家', '累计确诊', '累计死亡', '累计治愈', '现有确诊', '累计确诊增量'])
        for country in continent['subList']:
            temp_list2 = [country['country'], country['confirmed'], country['died'],
                          country['crued'], country['curConfirm'], country['confirmedRelative']]
            # print(temp_list2)
            ws_continent.append(temp_list2)

    # 由于上面全球数据和亚洲的数据都没有中国，所以我们需要单独获取中国疫情数据，添加到前两张表中
    ws_global = wb['全球各个国家疫情数据']
    ws_asia = wb['亚洲疫情数据']
    china_data = data['component'][0]['summaryDataIn']
    # print(china_data)
    """
    {'confirmed': '7674690', 'died': '27135', 'cured': '347107', 'asymptomatic': '14345', 
    'asymptomaticRelative': '1252', 'unconfirmed': '0', 'relativeTime': '1665504000', 
    'confirmedRelative': '53519', 'unconfirmedRelative': '0', 'curedRelative': '496', 
    'diedRelative': '46', 'icu': '13', 'icuRelative': '3', 'overseasInput': '24818', 
    'unOverseasInputCumulative': '7649872', 'overseasInputRelative': '50', 
    'unOverseasInputNewAdd': '322', 'curConfirmLocal': '4397', 'curOverseasInput': '618', 
    'curLocalConfirm': '3779', 'asymptomaticLocalRelative': '1154', 'specialAdministrativeRegionRelative': '53147', 
    'curOverseasInputAsymptomatic': '890', 
    'asymptomaticLocal': '13455', 'curConfirm': '7300448', 'curConfirmRelative': '52977', 'icuDisable': '1'}
    """
    relative_time2 = china_data['relativeTime']
    relative_time2 = time.strftime('%Y-%m-%d %H-%M-%S', time.localtime(int(relative_time2)))

    # 将中国数据添加全球疫情数据表中
    ws_global.append(['中国', china_data['confirmed'], china_data['died'], china_data['cured'],
                      china_data['curConfirm'], china_data['confirmedRelative'], relative_time2])

    # 将中国数据添加到亚洲疫情数据表中
    ws_asia.append(['中国', china_data['confirmed'], china_data['died'], china_data['cured'],
                    china_data['curConfirm'], china_data['confirmedRelative']])

    # 创建一个工作表获取境外的更新时间
    ws_foreign = wb.create_sheet("境外疫情更新时间")
    ws_foreign.column_dimensions['A'].width = 23
    ws_foreign.append(['境外疫情数据更新时间'])
    ws_foreign.append([data['component'][0]['foreignLastUpdatedTime']])

    wb.save("COVID-19-global.xlsx")
    print("全球疫情数据保存完毕！！")


def china_day_data(data):
    """
    china_day_datas = data['component'][0]['trend']
    china_day_datas['updateDate']：  日期
    china_day_datas['list'][0]['data']：确诊
    china_day_datas['list'][1]['data']：疑似
    china_day_datas['list'][2]['data']：治愈
    china_day_datas['list'][3]['data']：死亡
    china_day_datas['list'][4]['data']：新增确诊
    china_day_datas['list'][5]['data']：新增疑似
    china_day_datas['list'][6]['data']：新增治愈
    china_day_datas['list'][7]['data']：新增死亡
    china_day_datas['list'][8]['data']：累计境外输入
    china_day_datas['list'][9]['data']：新增境外输入
    china_day_datas['list'][10]['data']：新增本土
    china_day_datas['list'][11]['data']：新增无症状
    china_day_datas['list'][12]['data']：新增本土无症状
    """
    # 获取中国疫情数据工作簿
    wb = openpyxl.load_workbook("COVID-19-china.xlsx")

    china_day_datas = data['component'][0]['trend']
    dates = china_day_datas['updateDate']
    confirmed = china_day_datas['list'][0]['data']
    yisi = china_day_datas['list'][1]['data']
    crued = china_day_datas['list'][2]['data']
    died = china_day_datas['list'][3]['data']
    confirmed_relative = china_day_datas['list'][4]['data']
    yisi_relative = china_day_datas['list'][5]['data']
    crued_relative = china_day_datas['list'][6]['data']
    died_relative = china_day_datas['list'][7]['data']
    jingwai = china_day_datas['list'][8]['data']
    overseasInputRelative = china_day_datas['list'][9]['data']
    unOverseasInputNewAdd = china_day_datas['list'][10]['data']
    asymptomaticRelative = china_day_datas['list'][11]['data']
    asymptomaticBentu = china_day_datas['list'][12]['data']

    # 创建一张表存储每日确诊
    ws_confirmed = wb.create_sheet("中国每日累计确诊数据")
    ws_confirmed.append(['日期', '数据'])
    for ws_confirmed2 in zip(dates, confirmed):
        ws_confirmed.append(ws_confirmed2)

    ws_yisi = wb.create_sheet("中国每日疑似数据")
    ws_yisi.append(['日期', '数据'])
    for ws_yisi2 in zip(dates, yisi):
        ws_yisi.append(ws_yisi2)

    ws_crued = wb.create_sheet("中国每日累计治愈数据")
    ws_crued.append(['日期', '数据'])
    for ws_crued2 in zip(dates, crued):
        ws_crued.append(ws_crued2)

    ws_died = wb.create_sheet("中国每日累计死亡数据")
    ws_died.append(['日期', '数据'])
    for ws_died2 in zip(dates, died):
        ws_died.append(ws_died2)

    ws_confirmed_relative = wb.create_sheet("中国每日新增确诊数据")
    ws_confirmed_relative.append(['日期', '数据'])
    for ws_confirmed_relative2 in zip(dates, confirmed_relative):
        ws_confirmed_relative.append(ws_confirmed_relative2)

    ws_yisi_relative = wb.create_sheet("中国每日新增疑似数据")
    ws_yisi_relative.append(['日期', '数据'])
    for ws_yisi_relative2 in zip(dates, yisi_relative):
        ws_yisi_relative.append(ws_yisi_relative2)

    ws_crued_relative = wb.create_sheet("中国每日新增治愈数据")
    ws_crued_relative.append(['日期', '数据'])
    for ws_crued_relative2 in zip(dates, crued_relative):
        ws_crued_relative.append(ws_crued_relative2)

    wb.save("COVID-19-china.xlsx")
    print("中国每日数据保存完毕！！")


# allForeignTrend
def foreign_day_data(data):
    """
    foreign_day_datas = data['component'][0]['allForeignTrend']
    foreign_day_datas['updateDate']:日期
    foreign_day_datas['list'][0]['data']:累计确诊
    foreign_day_datas['list'][1]['data']:治愈
    foreign_day_datas['list'][2]['data']:死亡
    foreign_day_datas['list'][3]['data']:现有确诊
    foreign_day_datas['list'][4]['data']:新增确诊
    foreign_day_datas['list'][5]['data']:新增死亡
    """
    # 获取原有的全球的工作簿
    wb = openpyxl.load_workbook("COVID-19-global.xlsx")
    foreign_day_datas = data['component'][0]['allForeignTrend']
    dates = foreign_day_datas['updateDate']
    confirmed = foreign_day_datas['list'][0]['data']
    crued = foreign_day_datas['list'][1]['data']
    died = foreign_day_datas['list'][2]['data']
    curConfirm = foreign_day_datas['list'][3]['data']
    confirmedRelative = foreign_day_datas['list'][4]['data']
    addDied = foreign_day_datas['list'][5]['data']

    ws_confirmed = wb.create_sheet("境外每日累计确诊")
    for ws_confirmed2 in zip(dates, confirmed):
        ws_confirmed.append(ws_confirmed2)

    ws_crued = wb.create_sheet("境外每日累计治愈")
    for ws_crued2 in zip(dates, crued):
        ws_crued.append(ws_crued2)

    ws_died = wb.create_sheet("境外每日累计死亡")
    for ws_died2 in zip(dates, died):
        ws_died.append(ws_died2)

    ws_curConfirm = wb.create_sheet("境外每日现有确诊")
    for ws_curConfirm2 in zip(dates, curConfirm):
        ws_curConfirm.append(ws_curConfirm2)

    ws_confirmedRelative = wb.create_sheet("境外每日新增确诊")
    for ws_confirmedRelative2 in zip(dates, confirmedRelative):
        ws_confirmedRelative.append(ws_confirmedRelative2)

    ws_addDied = wb.create_sheet("境外每日新增死亡")
    for ws_addDied2 in zip(dates, addDied):
        ws_addDied.append(ws_addDied2)

    wb.save("COVID-19-global.xlsx")


if __name__ == '__main__':
    # 程序的开始
    data = get_data_requests()
    china_total_data(data)
    global_total_data(data)
    china_day_data(data)
    foreign_day_data(data)
